﻿using Common;
using Common.Enums.OpticalEnums;
using Newtonsoft.Json;
using OpticalService;
using RcsService;
using RollerControllerService;
using Serilog;
using System.Collections.Concurrent;
using System.Text;

namespace OpticalComService.Services
{
    /// <summary>
    /// 整个系统初期化管理服务
    /// </summary>
    public class OpticalInitService
    {
        #region 单例
        public static OpticalInitService Instance = new Lazy<OpticalInitService>(() => new OpticalInitService()).Value;
        private OpticalInitService() { }
        #endregion

        #region 属性
        private TransferRcsService transferRcsService;
        private TransferOpticalService transferOpticalService;
        private TransferRollerService transferRollerService;
        private ConcurrentDictionary<string, Func<string, string, Task>> WaitApiQueue = new ConcurrentDictionary<string, Func<string, string, Task>>();
        #endregion

        public void ServiceInitProcess()
        {
            //Rcs通信服务
            transferRcsService = new TransferRcsService(ResultProcessAsync);
            //光通信服务
            transferOpticalService = new TransferOpticalService(transferRcsService.MsgProcess);


            transferRollerService = new TransferRollerService();
            transferRcsService.RollerControlEvent += transferRollerService.WcsMsgProcessAsync;
            transferRollerService.ResultRollerInfoChange += TransferRollerService_ResultRollerInfoChange;
        }

        
        public async Task<bool> SendOpticalMsgProcess(string para)
        {
            return await transferOpticalService.SendMsgProcsssAsync(para);
        }
        /// <summary>
        /// 添加至等待WCS回复队列
        /// </summary>
        /// <param name="reqCode"></param>
        /// <returns></returns>
        public async Task<bool> AddWaitResultQueueAsync(string reqCode, Func<string, string, Task> execute)
        {
            return await Task.Run(() => 
            {
                var value = WaitApiQueue.AddOrUpdate(reqCode, execute, (key, value) => value);
                return true;
            });
        }
        /// <summary>
        /// 移除等待队列
        /// </summary>
        /// <returns></returns>
        public async Task RemoveWaitQueueAsync(string reqCode)
        {
            if (string.IsNullOrEmpty(reqCode))
            {
                return;
            }
            if (!WaitApiQueue.ContainsKey(reqCode))
            {
                return;
            }
            if (!WaitApiQueue.TryGetValue(reqCode, out Func<string, string, Task> value))
            {
                return;
            }
            KeyValuePair<string, Func<string, string, Task>> removeValue = 
                new KeyValuePair<string, Func<string, string, Task>>(reqCode, value);
            WaitApiQueue.TryRemove(removeValue);
        }

        /// <summary>
        /// 服务访问后返回值处理
        /// </summary>
        /// <returns></returns>
        private async Task ResultProcessAsync(string reqCode, OpticalMsgType type, string info)
        {
            switch (type)
            {
                case OpticalMsgType.Result:
                    await ResultFromWcsAsync(reqCode, info);
                    break;
                case OpticalMsgType.call:
                    await ResultFromRcsAsync(reqCode, info);
                    break;
                default:
                    await UnKnowResultAsync(reqCode, info);
                    break;
            }
        }

        /// <summary>
        /// 接收的从WCS服务返回的数据报文
        /// </summary>
        /// <param name="reqCode"></param>
        /// <param name="info"></param>
        /// <returns></returns>
        private async Task ResultFromWcsAsync(string reqCode,string info)
        {
            if (string.IsNullOrEmpty(reqCode))
            {
                return;
            }
            if (!WaitApiQueue.ContainsKey(reqCode))
            {
                return;
            }
            if (!WaitApiQueue.TryGetValue(reqCode, out Func<string,string, Task> execute))
            {
                return;
            }
            await execute(reqCode,info);
        }
        /// <summary>
        /// 接收的从RCS服务返回的数据报文
        /// </summary>
        /// <param name="reqCode"></param>
        /// <param name="info"></param>
        /// <returns></returns>
        private async Task ResultFromRcsAsync(string reqCode, string info)
        {
            await SendOpticalMsgProcess(info);
        }

        private async Task UnKnowResultAsync(string reqCode, string info)
        {
            await Task.Run(() => 
            {
                Log.Error($"未知返回值{reqCode}--{info}");
            });
        }

        private async Task TransferRollerService_ResultRollerInfoChange(string arg)
        {
            await transferOpticalService.SendMsgProcsssAsync(arg);
        }

    }
}
